public class Test01 {
    private static int[] tmp;
    public int[] sortArray(int[] nums) {
        tmp = new int[nums.length];
        mergeSort(nums, 0, nums.length - 1);
        return nums;
    }

    private static void mergeSort(int[] nums, int left, int right){
        if(left >= right) return;
        int mid = (left + right) / 2;
        //划分
        mergeSort(nums, left, mid);
        mergeSort(nums, mid + 1, right);
        //合并
        int curA = left, curB = mid + 1; int i = 0;
        while(curA <= mid && curB <= right ){
            if(nums[curA] > nums[curB]) tmp[i++] = nums[curB++];
            else tmp[i++] = nums[curA++];
        }
        while(curA <= mid){
            tmp[i++] = nums[curA++];
        }
        while(curB <= right){
            tmp[i++] = nums[curB++];
        }
        //合并完毕，辅助数组放到排序数组中
        for(int j = left; j <= right; j++){
            nums[j] = tmp[j - left];
        }
    }
}
